﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Client.Forms
{
    public partial class AddZXGForm : Form
    {
        DataCenter.Models.StockInfo stockInfo;

        public AddZXGForm(DataCenter.Models.StockInfo stockInfo)
        {
            InitializeComponent();
            this.stockInfo = stockInfo;
        }

        private void AddZXGForm_Load(object sender, EventArgs e)
        {
            this.Text = "将" + stockInfo.name + "添加到自选分类";

            foreach (var i in Managers.Manager.zxgM.GetKindNames())
            {
                this.listBox1.Items.Add(i);
            }

            this.listBox1.SelectedIndex = 0;
            this.listBox1.Focus();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Add("");
            this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
            var ra = this.listBox1.GetItemRectangle(this.listBox1.Items.Count-1);
            textBox1.Location = new Point(ra.Location.X + this.listBox1.Location.X + 5, ra.Location.Y + this.listBox1.Location.Y+2);
            textBox1.Visible = true;
            textBox1.Focus();
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                if (!Managers.Manager.zxgM.Exist(textBox1.Text))
                {
                    this.listBox1.Items[this.listBox1.Items.Count - 1] = textBox1.Text;
                    Managers.Manager.zxgM.AddKind(textBox1.Text);
                    textBox1.Text = "";
                    textBox1.Visible = false;
                    this.listBox1.Focus();
                }
                else
                {
                    MessageBox.Show("已存在名为" + textBox1.Text + "的分类。", "提示");
                    textBox1.Text = "";
                    textBox1.Focus();
                }
            }
            else
            {
                this.listBox1.Items.RemoveAt(this.listBox1.Items.Count - 1);
                textBox1.Visible = false;
                this.listBox1.Focus();
            }
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex > 0)
            {
                Managers.Manager.zxgM.RemoveKind((string)this.listBox1.Items[this.listBox1.SelectedIndex]);
                this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
                this.listBox1.Focus();
            }
            else if(this.listBox1.SelectedIndex==0)
            {
                MessageBox.Show("自选股分类不可删除。", "提示");
            }
            else
            {
                MessageBox.Show("没有选择任何项。", "提示");
            }

        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex != -1)
            {
                string kindName = (string)this.listBox1.Items[this.listBox1.SelectedIndex];
                if (!Managers.Manager.zxgM.Exist(kindName, stockInfo.symbol))
                {
                    Managers.Manager.zxgM.zxKind = kindName;
                    Managers.Manager.zxgM.AddStock(stockInfo.symbol);
                    Managers.Manager.clientForm.InitZXGMenu();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("此分类下已存在相同股票。", "提示");
                }
            }
            else
            {
                MessageBox.Show("请先选择分类。", "提示");
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                this.listBox1.Focus();
            else if (e.KeyCode == Keys.Escape)
                this.listBox1.Focus();
        }
    }
}
